<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    了解 generator,学习成本高
    迭代 (循环)、地狱回调问题
    async await 是 generator 语法糖
    <script>
        //1.基础语法
        // function* hello() {
        //     console.log("generator运行了");
        // }
        // //函数名前面加了*  ，调用时需要使用  .next()
        // hello().next();

        //2.迭代器
        // function* gen() {
        //     yield "hello";
        //     yield "hi";
        //     yield "lili";
        //     yield "666";
        //     return;
        // }
        // var g = gen();
        // console.log(g.next());//{value: 'hello', done: false}  值：hello ，是否结束了：false 迭代还没结束
        // console.log(g.next());
        // console.log(g.next());
        // console.log(g.next());
        // console.log(g.next());//{ value: undefined, done: true }

        //迭代器循环完之后，最后一个 undefined不会输出
        // var h = gen();
        // for (var value of h) {
        //     console.log(value); //
        // }

        //实例
        // function* gen(x, y) {
        //     let z = yield x + y;
        //     console.log("z:" + z);
        //     let result = yield z * x;
        //     return result;
        // }
        // var g = gen(5, 6);
        // console.log(g.next());//11
        // console.log(g.next());//{done: false,value: NaN}



        function* gen(x, y) {
            let z = yield x + y;
            console.log("z:" + z);
            let result = yield z * x;
            return result;
        }
        var g = gen(5, 6);
        let nn = g.next();
        // console.log(g.next());//11
        console.log(g.next(nn.value));//{done: false,value: 55}
    </script>
</body>

</html>